package queryTree;

import java.util.*;

import optimization.Condition;

/**
 * TableNode is a node in tree including table_name, projection on that table
 * and selection predicate in that table.
 * @author Van
 *
 */
public class TableNode extends AbstractNode {
	String tableName;
	
	
	public TableNode() {
		super();
	}
	public TableNode(String tableName) {
		super();
		this.tableName = tableName;
	}
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	
	public Object clone() throws CloneNotSupportedException
	{
		TableNode newObject= new TableNode();
		if(tableName != null) {
			newObject.setTableName(this.tableName);
		} else if(AttrsProjection != null) {
			newObject.setAttrsProjection(AttrsProjection);
		} else if(selectPredicate != null) {
			newObject.setSelectPredicate(selectPredicate);
		}
		
		return newObject;
	}
	public void getTableList(ArrayList<String> tableList) {
		// TODO Auto-generated method stub
		tableList.add(tableName);
	}
	public String toString()
	{
		return tableName;
	}
	public String getLocalSQL() {
		String result = "";
		if(AttrsProjection!=null) {
			for(int i = 0; i < AttrsProjection.size(); i ++) {
				if(i == 0) {
					result = "SELECT " + AttrsProjection.get(i).item_name.toString() + " ";
				} else {
					result += "," + AttrsProjection.get(i).item_name.toString() + " ";
				}
			}
		}
		result += "FROM " + tableName + " ";
		if(selectPredicate != null) {
			result += "WHERE " + selectPredicate.toString();
		}
		return result;
	}

}
